<div class="layui-card layadmin-header">
  <div class="layui-breadcrumb" lay-filter="breadcrumb">
    <a><cite>权益管理</cite></a>
    <a><cite>实物权益</cite></a>
  </div>
</div>

<div class="layui-fluid">
  <div class="layui-card">
    <div class="layadmin-search">
      <form class="layui-form" method="post" lay-filter="form_entity_search">
        <script type="text/html" template>
          <div class="layui-inline">
            <div class="layui-form-label">关键字搜索</div>
            <div class="layui-input-inline">
              <input type="text" name="keyword" value="{{decodeURIComponent(layui.router().search.keyword || '')}}"
                     class="layui-input"
                     placeholder="请输入名称">
            </div>
          </div>
          <div class="layui-inline layui-btn-group">
            <button type="submit" lay-submit class="layui-btn" lay-filter="submit_entity_search">
              <i class="layui-icon layui-icon-search"></i>查询
            </button>
            <button type="submit" lay-submit class="layui-btn layui-btn-warm"
                    lay-filter="reset_entity_search">
              <i class="layui-icon layui-icon-close"></i>重置
            </button>
          </div>
        </script>
          <button id="btn_add_entity" class="layui-btn layui-btn-normal">
            <i class="layui-icon layui-icon-add-1"></i>创建实物
          </button>
      </form>
    </div>
    <div class="layui-card-body">
      <div id="table_entity"></div>
      <script type="text/html" id="tpl_status">
        {{# if (d.status.value === 'on_sale') { }}
        <span class="layui-font-green">{{d.status.description}}</span>
        {{# } }}
        {{# if (d.status.value === 'pause') { }}
        <span class="layui-font-orange">{{d.status.description}}</span>
        {{# } }}
      </script>
      <script type="text/html" id="tpl_tool">
        <a class="layui-table-link" lay-href="rights/apply/name={{d.name}}"><i class="layui-icon layui-icon-table"></i>查看</a>
        <div class="ant-divider"></div>
        <a class="layui-table-link" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>
        <div class="ant-divider"></div>
        <a class="layui-table-link layui-font-red" lay-event="del"><i class="layui-icon layui-icon-delete"></i>删除</a>
        <div class="ant-divider"></div>
        {{# if (d.status.value === 'on_sale') { }}
        <a class="layui-table-link layui-font-orange" lay-event="pause">暂停</a>
        {{# } }}
        {{# if (d.status.value === 'pause') { }}
        <a class="layui-table-link layui-font-blue"  lay-event="sale">开启</a>
        {{# } }}
      </script>
    </div>
  </div>
</div>

<script>
  layui.use(['fox_table'], function () {
    let $ = layui.$
      , fox_table = layui.fox_table
      , form = layui.form
      , admin = layui.admin
      , view = layui.view

    let resourceUrl = 'agent-admin/rights_entities'

    // 列表查询
    fox_table.render({
      id: 'table_entity'
      , url: resourceUrl
      , cols: [[
        {title: 'ID', field: 'id', width: 80, align: "center", sort: true}
        , {title: '实物名称', field: 'name', minWidth: 180}
        , {title: '创建时间', field: 'created_at', width: 180, align: "center"}
        , {title: '兑付数量', field: 'exchange_count', width: 100, align: "center"}
        , {title: '状态', templet: '#tpl_status', width: 100}
        , {title: '操作', width: 300, align: 'center', fixed: 'right', toolbar: '#tpl_tool'}
      ]]
    });

    //监听操作栏
    fox_table.toolbar(function (obj) {
      let data = obj.data;
      if (obj.event === 'del') {
        layer.confirm('确认删除该数据?', function (index) {
          admin.del(resourceUrl, data.id, function (res) {
            layer.close(index);
            fox_table.reload();
          })
        });
      }
      if (obj.event === 'pause') {
        layer.confirm('确认暂停该实物权益吗?', function (index) {
          admin.post(resourceUrl+ '/' + data.id + '/pause', data.id, function (res) {
            layer.close(index);
            fox_table.reload();
          })
        });
      }
      if (obj.event === 'sale') {
        layer.confirm('确认发行该实物权益吗?', function (index) {
          admin.post(resourceUrl+ '/' + data.id + '/sale', data.id, function (res) {
            layer.close(index);
            fox_table.reload();
          })
        });
      }
      if (obj.event === 'edit') {
        showForm(data);
      }
    });
    //添加
    $('#btn_add_entity').click(function () {
      showForm();
      return false;
    });

    //搜索
    form.on('submit(submit_entity_search)', function (d) {
      fox_table.reload(d.field);
      return false;
    });
    //重置
    form.on('submit(reset_entity_search)', function (d) {
      $("input[name='keyword']").val('');
      fox_table.reload({});
      return false;
    });
    //编辑
    function showForm(data) {
      data = data || {};
      let minWidth = '800px';
      let minHeight = '750px';
      admin.popup({
        title: (data.id ? '修改' : '添加') + '实物'
        , area: [minWidth, minHeight]
        , id: 'popup_entity'
        , success: function (layero, index) {
          view(this.id).render('rights/entity_edit', data).done(function () {
            form.on('submit(submit_entity)', function (d) {
              if (!d.field.logo_url) {
                layer.msg('请上传轮实物主图')
                return;
              }

              layer.load();
              let field = d.field;
              admin.post(resourceUrl, field, function (res) {
                layer.closeAll();
                fox_table.reload();
              }, {hasLoading: true})
            })
          })
        }
      })
    }
  })
</script>

